package com.admin.service;
import com.admin.dao.DrugDao;
import com.admin.entity.Drug;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.admin.sys.utils.admin.StringUtils;
import com.admin.sys.utils.admin.ObjectUtils;
import com.admin.sys.base.module.extend.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

@Service
public class DrugService extends BaseService {

    @Autowired
    private DrugDao drugDao;

    private LambdaQueryWrapper<Drug> getDrugQueryCondition(Drug drug) {
        LambdaQueryWrapper<Drug> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.orderByDesc(Drug::getCreateDate);
        if (ObjectUtils.isNotNull(drug)) {
            lambdaQuery.eq(StringUtils.isNotEmpty(drug.getId()), Drug::getId, drug.getId());
            lambdaQuery.like(StringUtils.isNotEmpty(drug.getDrugName()), Drug::getDrugName, drug.getDrugName());
            lambdaQuery.like(StringUtils.isNotEmpty(drug.getDrugKey()), Drug::getDrugKey, drug.getDrugKey());
            // 其他查询条件根据需求添加
        }
        return lambdaQuery;
    }

    public IPage<Drug> getPage(Page<Drug> page, Drug drug) {
        LambdaQueryWrapper<Drug> lambdaQuery = getDrugQueryCondition(drug);
        return drugDao.selectPage(page, lambdaQuery);
    }

    public List<Drug> getList(Drug drug) {
        LambdaQueryWrapper<Drug> lambdaQuery = getDrugQueryCondition(drug);
        return drugDao.selectList(lambdaQuery);
    }

    public Drug get(Drug drug) {
        LambdaQueryWrapper<Drug> lambdaQuery = getDrugQueryCondition(drug);
        return drugDao.selectOne(lambdaQuery);
    }

    public int insert(Drug drug) {
        return drugDao.insert(drug);
    }

    public int update(Drug drug) {
        return drugDao.updateById(drug);
    }

    public int delete(String id) {
        return drugDao.deleteById(id);
    }

    public int delAll(String[] ids) {
        return drugDao.deleteBatchIds(Arrays.asList(ids));
    }
}

//
//import com.admin.dao.DrugDao;
//import com.admin.entity.Drug;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.admin.sys.utils.admin.StringUtils;
//import com.admin.sys.utils.admin.ObjectUtils;
//import com.admin.sys.base.module.extend.service.BaseService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import java.util.Arrays;
//import java.util.List;
//
//@Service
//public class DrugService extends BaseService {
//
//    @Autowired
//    private DrugDao drugDao;
//
//    private LambdaQueryWrapper<Drug> getDrugQueryCondition(Drug drug) {
//        LambdaQueryWrapper<Drug> lambdaQuery = new LambdaQueryWrapper<>();
//        lambdaQuery.orderByDesc(Drug::getCreateDate);
//        if (ObjectUtils.isNotNull(drug)) {
//            lambdaQuery.eq(StringUtils.isNotEmpty(drug.getDrugId()), Drug::getDrugId, drug.getDrugId());
//            lambdaQuery.like(StringUtils.isNotEmpty(drug.getDrugName()), Drug::getDrugName, drug.getDrugName());
//            // 其他查询条件根据需求添加
//        }
//        return lambdaQuery;
//    }
//
//    public IPage<Drug> getPage(Page<Drug> page, Drug drug) {
//        LambdaQueryWrapper<Drug> lambdaQuery = getDrugQueryCondition(drug);
//        return drugDao.selectPage(page, lambdaQuery);
//    }
//
//    public List<Drug> getList(Drug drug) {
//        LambdaQueryWrapper<Drug> lambdaQuery = getDrugQueryCondition(drug);
//        return drugDao.selectList(lambdaQuery);
//    }
//
//    public Drug get(Drug drug) {
//        LambdaQueryWrapper<Drug> lambdaQuery = getDrugQueryCondition(drug);
//        return drugDao.selectOne(lambdaQuery);
//    }
//
//    public int insert(Drug drug) {
//        return drugDao.insert(drug);
//    }
//
//    public int update(Drug drug) {
//        return drugDao.updateById(drug);
//    }
//
//    public int delete(String drugId) {
//        return drugDao.deleteById(drugId);
//    }
//
//    public int delAll(String[] drugIds) {
//        return drugDao.deleteBatchIds(Arrays.asList(drugIds));
//    }
//}
//
